#include "CreateTool.h"
#include "EditWidget.h"

using namespace Editor;

CreateTool::CreateTool(Editor::EditWidget* w) 
    : widget(w), currentVertex(0)
{
}

void CreateTool::leftButtonPressed()
{
    if(currentVertex)
        currentVertex->setSelected(false);
    
    currentVertex = widget->getNearVertex(mousePos);

    if(currentVertex)
        currentVertex->setSelected(true);
    
    emit sceneChanged();
}

void CreateTool::leftButtonReleased()
{
}

void CreateTool::rightButtonPressed()
{
    if(!currentVertex) 
    {
        setCurrentVertex(widget->addVertex(mousePos));
        return;
    }
    
    VertexItem* item = widget->getNearVertex(mousePos);
    if(!item)
        item = widget->addVertex(mousePos);
    
    widget->addEdge(currentVertex->vertex(), item->vertex());
    setCurrentVertex(item);
    emit sceneChanged();
}

void CreateTool::setCurrentVertex(VertexItem* item)
{
    if(currentVertex)
        currentVertex->setSelected(false);
    
    currentVertex = item;
    
    if(currentVertex)
        currentVertex->setSelected(true);
}

void CreateTool::rightButtonReleased()
{
}

void CreateTool::keyPressEvent(QKeyEvent* e)
{
    if(e->key() == Qt::Key_Delete)
        if(currentVertex)
        {
            widget->removeVertex(currentVertex);
            setCurrentVertex(0);
        }
        
    emit sceneChanged();
}

